package com.example.demo.controller;
import com.example.demo.model.Student;
//import com.example.demo.model.Teacher;
import com.example.demo.repository.StudentRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.data.domain.Example;
import com.example.demo.service.StudentService;
import java.util.ArrayList;
import java.util.List;
@Controller
@RequestMapping("student")
public class StudentController {
    @Autowired
    private StudentService studentService;
    //@Autowired
    //private StudentRepository studentRepository;

    /*private static List<Student> studentList = new ArrayList<>();
    static {
        Student s1 = new Student();
        s1.setId(1);
        s1.setName("Tom");
        studentList.add(s1);
        Student s2 = new Student();
        s2.setId(2);
        s2.setName("Jerry");
        studentList.add(s2);
    }*/


    @RequestMapping("list")
    public String list(ModelMap modelMap) {
        modelMap.put("studentList", studentService.list());
        /*modelMap.put("studentList", studentRepository.findAll());*/
        return "student/list";
    }

    @RequestMapping("delete/{id}")
    public String delete(@PathVariable Integer id) {
        studentService.delete(id);
        /*studentRepository.deleteById(id);*/
        //studentList.remove(id - 1);
        return "redirect:/student/list";
    }

    @RequestMapping("save")
    public String save(Student s,ModelMap modelMap) {
        try {
            studentService.save(s);
        } catch (Exception e) {
            e.printStackTrace();
            modelMap.put("error",e);
            return "error";
        }
        /*if (studentRepository.findByTel(s.getTel()).size() > 0) {
            return "error";
        }
        studentRepository.save(s);*/
        //s.setId(studentList.size() + 1);
        //studentList.add(s);
        return "redirect:/student/list";
    }

    @RequestMapping("edit/{id}")
    public String edit(@PathVariable Integer id, ModelMap map) {
        map.put("student", studentService.get(id));
        /*Student s = studentRepository.getReferenceById(id);*/
        //Student s = studentList.get(id - 1);
        /*map.put("student", s);*/
        return "student/edit";
    }

    @RequestMapping("update")
    public String update(Student s,ModelMap modelMap) {
        try {
            studentService.update(s);
        } catch (Exception e) {
            e.printStackTrace();
            modelMap.put("error",e);
            return "error";
        }
        /*List<Student> students = studentRepository.findByTel(s.getTel());
        if (students.size() > 1) {
            return "error";
        } else if (students.size() == 1) {
            if (!students.get(0).getId().equals(s.getId())) {
                return "error";
            }
        }
        studentRepository.save(s);*/
        //for (Student student: studentList) {
        //   if (student.getId().equals(s.getId())) {
        //        student.setName(s.getName());
        //    }
        //}
        return "redirect:/student/list";
    }

    @RequestMapping("search")
    public String search(Student s, ModelMap modelMap) {
        modelMap.put("studentList", studentService.search(s));
        /*List<Student> studentList = studentRepository.findAll(Example.of(s));*/
        //List<Student> studentList = studentRepository.findByNativeQuery(s.getName());
        //List<Student> studentList = studentRepository.findByName(s.getName());
        /*modelMap.put("studentList", studentList);*/
        return "student/list";
    }

}